from __future__ import division
from operator import itemgetter, attrgetter
from struct import *
import gc
import math
import matplotlib
import os
import pylab
import random
import sys
import time
from sets import Set
from scipy import stats
import numpy as np

print "Begins..."
docIDANDTrecIDDict = {}
inputFileName0 = "/home/diaosi/gov2ClearYourMindAndDoItAgain/trecID_docID_MappingTableForGov2Dataset"
inputFileHandler = open(inputFileName0,"r")
currentLine = inputFileHandler.readline()
while currentLine:
    currentLineElements = currentLine.strip().split(" ")
    currentDocID = currentLineElements[1]
    currentTrecID = currentLineElements[0]
    if currentDocID not in docIDANDTrecIDDict:
        docIDANDTrecIDDict[currentDocID] = currentTrecID
    currentLine = inputFileHandler.readline()
print "len(docIDANDTrecIDDict):",len(docIDANDTrecIDDict)
print "docIDANDTrecIDDict['0']:",docIDANDTrecIDDict['0']
inputFileHandler.close()

# AND
# inputFileName1 = "/home/diaosi/workspace/web-search-engine-wei-2014-March/results/makeOutputLinesForTrecEvalCompatibleFormat_20140502Night"
# OR
inputFileName1 = "/home/diaosi/workspace/web-search-engine-wei-2014-March/results/outputLinesForTrecEvalCompatible_our_approach_weight_50_OR_20140505Morning"
inputFileHandler = open(inputFileName1,"r")
outputBaseFileName = "/home/diaosi/workspace/web-search-engine-wei-2014-March/results/trecResult_trecEvalCompatible_OR_our_approach_weight_50"
fileNumber = 0
startToRecordTag = True
endToRecordTag = True
for line in inputFileHandler.readlines():
    if line.strip().endswith("PolyIRTKDebug"):
        if startToRecordTag:
            # print "new pruning level begins..."
            outputFileName = outputBaseFileName + "_" + str(fileNumber)
            outputFileHandler = open(outputFileName,"w")
            fileNumber += 1
            startToRecordTag = False
        # print line.strip()
        lineElements = line.strip().split(" ")
        currentDocID = lineElements[3]
        outputLine = lineElements[0][8:] + " " + lineElements[1] + " " + lineElements[2] + " " + docIDANDTrecIDDict[ lineElements[3] ] + " " + lineElements[4] + " " + lineElements[5] + " " + lineElements[6] + "\n"
        outputFileHandler.write(outputLine)
        
    if not line.strip().endswith("PolyIRTKDebug") and not startToRecordTag:
        if endToRecordTag:
            print "new pruning level ends."
            endToRecordTag = False
            # init 
            startToRecordTag = True
            endToRecordTag = True
            print "OVERALL:"
            print "inputFileName1:",inputFileName1
            print "outputFileName:",outputFileName
            outputFileHandler.close()

inputFileHandler.close()

print "OVERALL:"
print "inputFileName0:",inputFileName0
print "inputFileName1:",inputFileName1
print "Ends."



